﻿using RecruitStudents.Client.BLL;
using RecruitStudents.Utility;
using RecruitStudents.Utility.Helper;
using RecruitStudents.Utility.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RecruitStudents.Client.Base
{
    public partial class Base_ExamPlace_SpecialityKinds_Group_Option : BaseForm
    {
        ServiceBase service = new ServiceBase();
        TB_Sys_ExamPlaceSpecialityKindsMapping model = null;
        TB_Sys_ExamPlaceSpecialityKindsGroupMapping modelGroup = null;
        string ExamPlaceSpecialityKindsMappingID = string.Empty;
        string GroupNo = string.Empty;
        string ExamPlaceNO = string.Empty;
        string SpecialityKindsNO = string.Empty;
        string ExamLevelNo = string.Empty;
        public Base_ExamPlace_SpecialityKinds_Group_Option(string strExamPlaceSpecialityKindsMappingID, string strGroupNo, string strExamLevelNo)
        {
            InitializeComponent();
            ExamLevelNo = strExamLevelNo;
            GroupNo = strGroupNo;
            ExamPlaceSpecialityKindsMappingID = strExamPlaceSpecialityKindsMappingID;
            BindExamLevelName();
            if (ExamPlaceSpecialityKindsMappingID.Length > 0)
            {
                model = service.GetExamPlaceSpecialityKindsMappingModel(ExamPlaceSpecialityKindsMappingID);
                if (model != null)
                {
                    model.Attach();
                    SpecialityKindsNO = model.SpecialityKindsNO;
                    ExamPlaceNO = model.ExamPlaceNO;
                }
            }
            if (GroupNo.Length > 0)
            {
                modelGroup = service.GetExamPlaceSpecialityKindsGroupMappingModel(ExamPlaceSpecialityKindsMappingID, GroupNo);
                if (modelGroup != null)
                {
                    modelGroup.Attach();
                    txtGroupNo.Text = modelGroup.GroupNo;
                    txtGroupNo.Enabled = false;
                    txtGroupName.Text = modelGroup.GroupName;
                    txtTotalAcount.Text = modelGroup.TotalAcount.ToSafeString();
                    txtActualAcount.Text = modelGroup.ActualAcount.ToSafeString();
                    txtActualAcount.Enabled = false;
                    txtOpenAcount.Text = modelGroup.OpenAcount.ToSafeString();
                    txtBeginNum.Text = modelGroup.BeginNum;
                    if (modelGroup.IsActive == "1")
                    {
                        chkSFKF.Checked = true;
                    }
                    else
                    {
                        chkSFKF.Checked = false;
                    }
                    txtSort.Text = modelGroup.Sort.ToSafeString();
                    txtLotNo_Prefix.Text = modelGroup.LotNo_Prefix;
                    txtLotNo_Len.Text = modelGroup.LotNo_Len.ToSafeString();
                }
            }
            BindExamPlace();
            BindSpecialityKinds();
        }

        private void BindSpecialityKinds()
        {
            if (SpecialityKindsNO.Length > 0)
            {
                TB_Sys_SpecialityKinds modelSpec = service.GetSpecialityKindsModel(SpecialityKindsNO);
                if (modelSpec != null)
                {
                    txt_SpecialityKindsID.Text = modelSpec.SpecialityKindsID;
                    txt_SpecialityKindsNO.Text = modelSpec.SpecialityKindsNO;
                    txt_SpecialityKindsName.Text = modelSpec.SpecialityKindsName;
                    txt_Sort.Text = modelSpec.Sort.ToSafeString();
                    if (modelSpec.IsActive == "1")
                    {
                        chkSFYX.Checked = true;
                    }
                    else
                    {
                        chkSFYX.Checked = false;
                    }
                    if (modelSpec.IsSchool == "1")
                    {
                        chkSFXK.Checked = true;
                    }
                    else
                    {
                        chkSFXK.Checked = false;
                    }
                    txt_SpecialityKindsID.Enabled = txt_SpecialityKindsName.Enabled = txt_SpecialityKindsNO.Enabled = txt_Sort.Enabled =
                        chkSFXK.Enabled = chkSFYX.Enabled = false;
                }
            }
        }

        private void BindExamPlace()
        {
            TB_Sys_ExamPlace modelPlace = service.GetSysExamPlaceModel(ExamPlaceNO);
            if (modelPlace != null)
            {
                txtExamPlaceNo.Text = modelPlace.ExamPlaceNO;
                txtExamPlaceName.Text = modelPlace.ExamPlaceName;
                txtExamPlaceNo.Enabled = txtExamPlaceName.Enabled = false;
            }
        }

        private void BindExamLevelName()
        {
            DataTable dt = service.GetDicValueByTableName("TB_Dic_ExamLevel");
            if (dt != null)
            {
                DataRow[] drs = dt.Select("DicNO='" + ExamLevelNo + "'");
                if (drs.Length > 0)
                {
                    txtExamLevelName.Text = drs[0]["DicName"].ToSafeString();
                    txtExamLevelName.Enabled = false;
                }
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (txtGroupNo.Text.Length == 0)
            {
                DevHelper.ShowInfoMessageBox("请先输入分组编号！");
                txtGroupNo.Focus();
                return;
            }
            if (txtGroupName.Text.Length == 0)
            {
                DevHelper.ShowInfoMessageBox("请先输入分组名称！");
                txtGroupName.Focus();
                return;
            }
            if (txtBeginNum.Text.Length == 0)
            {
                DevHelper.ShowInfoMessageBox("请先输入抽签序号！");
                txtBeginNum.Focus();
                return;
            }
            if (modelGroup == null)
            {
                modelGroup = new TB_Sys_ExamPlaceSpecialityKindsGroupMapping();
            }
            modelGroup.ActualAcount = txtActualAcount.Text.ToSafeInt();
            modelGroup.BeginNum = txtBeginNum.Text;
            modelGroup.ExamLevel = ExamLevelNo.ToSafeInt();
            modelGroup.ExamPlaceSpecialityKindsMappingID = ExamPlaceSpecialityKindsMappingID;
            modelGroup.GroupName = txtGroupName.Text;
            modelGroup.GroupNo = txtGroupNo.Text;
            modelGroup.OpenAcount = txtOpenAcount.Text.ToSafeInt();
            modelGroup.Sort = txtSort.Text;
            modelGroup.SpecialityKindsNO = SpecialityKindsNO;
            modelGroup.TotalAcount = txtTotalAcount.Text.ToSafeInt();
            modelGroup.ExamPlaceNO = ExamPlaceNO;
            modelGroup.ExamYear = SessionerHelper.SystemYear;
            modelGroup.LotNo_Len = txtLotNo_Len.Text.ToSafeInt();
            modelGroup.LotNo_Prefix = txtLotNo_Prefix.Text;
            if (chkSFKF.Checked)
            {
                modelGroup.IsActive = "1";
            }
            else
            {
                modelGroup.IsActive = "0";
            }

            if (service.SaveExamPlaceSpecialityKindsGroupMapping(modelGroup))
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }

        }
    }
}
